package com.city.parking;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TrafficViolationListActivity extends FragmentActivity implements
        OnClickListener {
	private List<ViolationItem> mViolationList = new ArrayList<ViolationItem>();
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private OrderListAdapter mAdapter;
	private Activity me;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        me = this;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_myviolation);
		TextView titleTv = (TextView) findViewById(R.id.nav_title);
	    titleTv.setText("我的违章");

        ImageView back = (ImageView) findViewById(R.id.nav_ico_back);
        back.setOnClickListener(this);

        ImageView more = (ImageView) findViewById(R.id.nav_ico_more);
        more.setVisibility(View.GONE);

		mViolationList.add(new ViolationItem("2015-10-02 13:05:48", "雨花西路小行路路口", "南京", "不按导向车道行驶", 3, 100, "已处理"));
		mViolationList.add(new ViolationItem("2016-04-08 10:25:09", "中山西路火车站北广场", "南京", "违章停车", 3, 100, "未处理"));
		mViolationList.add(new ViolationItem("2015-09-16 20:33:51", "沿江公路14公里500米", "南京", "超速10%以上,20%以下", 3, 200, "未处理"));

        ListView listView = (ListView) findViewById(R.id.violation_list);
		View header = getLayoutInflater().inflate(R.layout.violation_header, null);
		listView.addHeaderView(header, null, false);
        mAdapter = new OrderListAdapter(this);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int pos,
                    long arg3) {

            }

        });
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.nav_ico_back:
			finish();
            break;
		default:
			break;
		}
	}
	
	private class ViewHolder {
		TextView time;
		TextView addr;
		TextView city;
		TextView action;
		TextView score;
		TextView pay;
		TextView status;
	}
	
	private class OrderListAdapter extends BaseAdapter {
		private LayoutInflater inflater = null;

		public OrderListAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mViolationList.size();
		}

		@Override
		public Object getItem(int position) {
			return mViolationList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.violation_item, null);
				holder.time = (TextView) convertView
				        .findViewById(R.id.time);
				holder.addr = (TextView) convertView
				        .findViewById(R.id.addr);
				holder.city = (TextView) convertView
						.findViewById(R.id.city);
				holder.action = (TextView) convertView
				        .findViewById(R.id.action);
				holder.score = (TextView) convertView
						.findViewById(R.id.score);
				holder.pay = (TextView) convertView
						.findViewById(R.id.pay);
				holder.status = (TextView) convertView
                        .findViewById(R.id.status);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			ViolationItem item = (ViolationItem)getItem(position);
			if (item != null) {
				holder.time.setText(item.time);
				holder.addr.setText(item.addr);
				holder.action.setText(item.action);
				holder.city.setText(item.city);
				holder.score.setText(item.score+"分");
				holder.pay.setText(item.pay+"元");
				holder.status.setText(item.status);
			}
			return convertView;
		}
	}
	
}
